﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Expression;
using System.Collections;

namespace Model
{
    public class Relato
    {
        #region Atributos

        private string idRelato;
        private string assunto;
        private string descricao;
        private DateTime dataCadastro;
        private Cliente cliente;

        #endregion

        #region Propriedades

        public string IdRelato
        {
            get { return idRelato; }
            set { idRelato = value; }
        }
        

        public string Descricao
        {
            get { return descricao; }
            set { descricao = value; }
        }
       

        public DateTime DataCadastro
        {
            get { return dataCadastro; }
            set { dataCadastro = value; }
        }
        

        public Cliente Cliente
        {
            get { return cliente; }
            set { cliente = value; }
        }

        public string Assunto
        {
            get { return assunto; }
            set { assunto = value; }
        }

        #endregion

        #region Costrutores

        public Relato()
        {
        }

        public Relato(string pIdRelato, string pDescricao, DateTime pDataCadastro, Cliente pCLiente)
        {
            this.IdRelato = pIdRelato;
            this.Descricao = pDescricao;
            this.DataCadastro = pDataCadastro;
            this.Cliente = pCLiente;
        }
        #endregion

        
        #region Metodos

        #region Persist
        public Boolean Persist()
        {
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objSession.Save(this);
                objTransaction.Commit();
                objSession.Close();
                return true;
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
        }
        #endregion

        #region Update
        public Boolean Update()
        {
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objSession.Update(this);
                objTransaction.Commit();
                objSession.Close();
                return true;
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
        }
        #endregion

        #region Delete
        public static Boolean Delete(string pIdRelato)
        {
            Relato objRelato = null;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objRelato = (Relato)objSession.Load(typeof(Model.Relato), pIdRelato);
                objSession.Delete(objRelato);
                objTransaction.Commit();
                objSession.Close();
                return true;
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
        }
        #endregion

        #region RetriveObject
        public static Relato RetriveObject(string pIdRelato)
        {
            Relato objRelato = null;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objRelato = (Relato)objSession.Load(typeof(Model.Relato), pIdRelato);
                objSession.Close();
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
            return objRelato;
        }
        #endregion

        #region RetriveObjects
        public static IList RetriveObjects()
        {
            IList listRelatos;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                ICriteria criteria = objSession.CreateCriteria(typeof(Model.Relato));
                criteria.AddOrder(Order.Asc("Nome"));

                listRelatos = criteria.List();

                objSession.Close();
            }
            catch (Exception ex)
            {
                listRelatos = null;
                objTransaction.Rollback();
                throw ex;
            }
            return listRelatos;
        }
     

        
        public static IList RetriveObjects(Cliente pCliente)
        {
            IList listRelatos;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                ICriteria criteria = objSession.CreateCriteria(typeof(Model.Relato));
                criteria.Add(Expression.Eq("Cliente", pCliente))
                        .AddOrder(Order.Asc("Assunto"));
                listRelatos = criteria.List();
                objSession.Close();

            }
            catch (Exception ex)
            {
                listRelatos = null;
                objTransaction.Rollback();
                throw ex;
            }

            return listRelatos;
        }
        #endregion
      
        #endregion


    }
}
